{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39858a74",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    ">1. 有序的数据额结构--列表\n",
    "\n",
    ">2. 用户输入\n",
    "\n",
    ">3. 关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0dbd24c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_01_name = \"jon，python，张大炮，吴二凡\"\n",
    "\n",
    "# 如何将整个班的名字存放在一个变量里？\n",
    "# 全部输入后再处理时会不会很麻烦？\n",
    "# 怎样知道变量里共有多少个人名？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c143dc29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 怎样知道变量里共有多少个人名？\n",
    "len(网二_01_name)\n",
    "# len()只能输出字词总数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6c9846a",
   "metadata": {},
   "source": [
    "## 1.引入列表概念\n",
    "\n",
    ">概念：list可以放数值和字符，但通常要么一起放数值，要么一起放字符串。\n",
    "\n",
    ">部分案例：\n",
    ">>1. 取值：通过索引值取出对应内容\n",
    ">>2. 切片：通过索引区间取出对应的一些内容\n",
    ">>3. 通过in查询是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "77535c49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "86736e3d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张大炮', '小孟', '达达利亚'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网二_name_list = [\"张大炮\",\"小孟\",\"达达利亚\"]\n",
    "print(网二_name_list,type(网二_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3c2494c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张大炮'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出列表对应位次的名字\n",
    "网二_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a8f2641e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看列表总共的位次\n",
    "len(网二_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5f8fed6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网二： 张大炮\n",
      "网二： 小孟\n",
      "网二： 达达利亚\n"
     ]
    }
   ],
   "source": [
    "for a in 网二_name_list:\n",
    "    print(\"网二：\",a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fe462e1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 查询列表中是否存在某数据\n",
    "\"张大炮\"in 网二_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83bdd048",
   "metadata": {},
   "source": [
    "## 2.2 列表的方法（Function）--对列表数据进行操作\n",
    "\n",
    ">1. 增\n",
    ">2. 删\n",
    ">3. 改\n",
    ">4. 查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b3443aaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list = [\"张大炮\",\"小孟\",\"达达利亚\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "83c21dd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# append 增加， 增加元素到末尾\n",
    "网二_name_list.append(\"张智豪\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "546bd79e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张大炮', '小孟', '达达利亚', '张智豪']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "81772c15",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list.append(\"甘雨\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f57bdc2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张大炮', '小孟', '达达利亚', '张智豪', '甘雨']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "299466b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert增加元素到某个位置\n",
    "网二_name_list.insert(3,\"摩拉克斯\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f10cd858",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张大炮', '小孟', '达达利亚', '摩拉克斯', '张智豪', '甘雨']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "92c9b27c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "1367dc14",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'reomve'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[1;32mIn [42]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43m网二_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreomve\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m张智豪\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'reomve'"
     ]
    }
   ],
   "source": [
    "网二_name_list.reomve(\"张智豪\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "9afa9f48",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list = [\"张大炮\",\"小孟\",\"达达利亚\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "635b34ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小孟', '达达利亚']"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和 in 一起使用，先检查成员变量是否存在，随后再remove\n",
    "\n",
    "if \"张大炮\" in 网二_name_list:\n",
    "    网二_name_list.remove(\"张大炮\")\n",
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "f5504ad1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张大炮', '小孟']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pop 默认删除最后一位（倒数第一个）\n",
    "网二_name_list = [\"张大炮\",\"小孟\",\"达达利亚\"]\n",
    "网二_name_list.pop()\n",
    "网二_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "0a3d8d4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：移除重复的元素\n",
    "a_list = [1,1,12,33,4,1,2,11,4,1,1,33,33,42,77]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "c4951b34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 33, 4, 2, 11, 4, 33, 33, 42, 77]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路一：\n",
    "出现次数 = a_list.count(1)\n",
    "for i in range(出现次数):\n",
    "    if 1 in a_list:\n",
    "        a_list.remove(1)\n",
    "    a_list\n",
    "a_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "8f7f16e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_list = [1,1,12,33,4,1,2,11,4,1,1,33,33,42,77]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "6e1a8e5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 33\n",
      "10 33\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 1, 12, 4, 1, 2, 11, 4, 1, 1, 33, 42, 77]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路二： 用枚举 enumerate 找到目标元素的位置\n",
    "for i,v in enumerate(a_list):\n",
    "    if v == 33:\n",
    "        print(i,v)\n",
    "        a_list.pop(i)\n",
    "a_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7221303",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
